package uniLearn.control;

import java.sql.*;
import java.util.ArrayList;
import uniLearn.data.*;

public class UserControl extends DataControl {
	//Define
	//SQL String
	private static String SQL_LOGIN = "SELECT `name`, `userType` FROM `users` WHERE `id`=? AND `password`=PASSWORD(?);";
	private static String SQL_GET_USER = "SELECT `name`, `userType` FROM `users` WHERE `id`=?;";
	private static String SQL_USER_UNIT = "SELECT `unit`.`id`, `unit`.`name` FROM `users_unit`, `unit` WHERE `unit_id` = `unit`.`id` AND `user_id` = ?";
	//variable
	private Users loginUser;
	
	public UserControl() {
	}
	
	public UserControl(Users loginUser) {
		this.loginUser = loginUser;
	}
	
	//login process
	public boolean login(String userID, String pwd) throws SQLException, Exception {
		if(checkLength(userID, Users.ID_MAX_LENGTH) || checkLength(pwd))
			return false;
		//TRY to SELECT from database
		//return true if it can be selected
		this.startConnection();
		PreparedStatement preStmt = this.dbCon.preStmt(SQL_LOGIN);
		//set parameter
		preStmt.setString(1, userID);
		preStmt.setString(2, pwd);
		//Run SQL
		ResultSet rs = preStmt.executeQuery();
		//Check a record is found
		if(rs.next()) {
			String userName = rs.getString("name");
			UsersType userType = UsersType.fromString(rs.getString("userType"));
			//Create user object
			this.loginUser = new Users(userID, userName, userType);
		}
		rs.close();
		preStmt.close();
		return (this.loginUser != null);
	}
	
	//get back the login user for others usage
	public Users getLoginUser() {
		return loginUser;
	}
	
	/**
	 * @param userID id for target user
	 * @return the User object include the detail
	 * @throws Exception
	 */
	public Users getUser(String userID) throws Exception {
		if(checkLength(userID, Users.ID_MAX_LENGTH))
			throw new Exception("Incorrect User ID");
		
		Users aUser = null;
		this.startConnection();
		PreparedStatement preStmt = this.dbCon.preStmt(SQL_GET_USER);
		//set parameter
		preStmt.setString(1, userID);
		//Run SQL
		ResultSet rs = preStmt.executeQuery();
		//Check a record is found
		if(rs.next()) {
			String userName = rs.getString("name");
			UsersType userType = UsersType.fromString(rs.getString("userType"));
			//Create user object
			aUser = new Users(userID, userName, userType);
		} else {
			rs.close();
			preStmt.close();
			throw new Exception("User not found");
		}
		rs.close();
		preStmt.close();
		
		return aUser;
	}
	
	//get a list of units that the user handle or study in
	public ArrayList<Units> getUserUnit() throws Exception {
		ArrayList<Units> userUnit = new ArrayList<Units>();
		if(this.loginUser != null)
		{
			//Get unit from database
			this.startConnection();
			PreparedStatement preStmt = this.dbCon.preStmt(SQL_USER_UNIT);
			preStmt.setString(1, loginUser.getId());
			
			ResultSet rs = preStmt.executeQuery();
			
			while(rs.next()) {
				String id = rs.getString("id");
				String name = rs.getString("name");
				
				userUnit.add(new Units(id, name));
			}
			
			rs.close();
			preStmt.close();
		}
		return userUnit;
	}
}
